<template>
	<!--总容器-->
	<div class="wrapper">

		<!--header-->
		<header>
			<div class="back">
				<p @click="goBack">&lt;</p>
			</div>
			<p>商家信息</p>
		</header>

		<!--logo-->
		<div class="bsns-logo">
			<img :src="business.businessImg">
		</div>

		<!--info-->
		<div class="bsns-info">
			<h1>{{business.businessName}}</h1>
			<p>&#165;{{business.starPrice}}起送 &#165;{{business.deliveryPrice}}配送</p>
			<p>{{business.businessExplain}}</p>
		</div>

		<!--foodlist-->
		<ul class="foodList">
			<li v-for="(item,index) in foodArr">
				<div class="food-l">
					<img :src="item.foodImg">
					<div class="food-l-info">
						<h3>{{item.foodName}}</h3>
						<p>{{item.foodExplain}}</p>
						<p>&#165;{{item.foodPrice}}</p>
					</div>
				</div>
				<div class="food-r">
					<div>
						<i class="fa fa-minus-circle" @click="minus(index)" v-show="item.quantity!=0"></i>
					</div>
					<p><span v-show="item.quantity!=0">{{item.quantity}}</span></p>
					<div>
						<i class="fa fa-plus-circle" @click="add(index)"></i>
					</div>
				</div>
			</li>

		</ul>

		<!--cart-->
		<div class="cart">
			<div class="cart-l">
				<div class="cart-l-icon" v-bind:style="totalQuantity==0?'background-color:#505051':'background-color:#3190E8'">
					<i class="fa fa-shopping-cart"></i>
					<div class="cart-l-icon-quantity" v-show="totalQuantity!=0">{{totalQuantity}}</div>
				</div>
				<div class="cart-l-info">
					<p>&#165;{{totalPrice}}</p>
					<p>另需配送费{{business.deliveryPrice}}元</p>
				</div>
			</div>
			<div class="cart-r">
				<div class="cart-r-case1" v-show="totalPrice<business.starPrice" >
					&#165;{{business.starPrice}}起送
					</div>
				<div class="cart-r-case2" @click="toOrder" v-show="totalPrice>=business.starPrice" >
					去结算
					</div>
			</div>
		</div>
	</div>

</template>

<script>
	export default{
		name:'BusinessInfo',
		data(){
			return{
				businessId:this.$route.query.businessId,
				business:{},
				foodArr:[]
			}
		},
		created(){
			this.user = this.$getSessionStorage('user');
			
			//根据businessId查询商家信息
			this.$axios.post('BusinessController/getBusinessById',this.$qs.stringify({
				businessId:this.businessId
			})).then(response=>{
				this.business = response.data;
			}).catch(error=>{
				console.error(error);
			});
			//根据businessId查询所属食品信息
			this.$axios.post('FoodController/listFoodByBusinessId', this.$qs.stringify({
				businessId: this.businessId
			})).then(response => {
				this.foodArr = response.data;
				for (let i = 0; i < this.foodArr.length; i++) {
					this.foodArr[i].quantity = 0;
				}
				//如果已登录，那么需要去查询购物车中是否已经选购了某个食品
				if (this.user != null) {
					this.listCart();
				}
			}).catch(error => {
				console.error(error);
			});
			
		},
		methods:{
			    goBack() {
			      this.$router.go(-1);
			    },
			/*
			add(index){
				//登录验证
				if(this.user == null){
					this.$router.push({path:'/login'});
					return;
				}
				if(this.foodArr[index].quantity==0){
					//insert
					this.saveCart(index);
				}else{
					//update
					this.updateCart(index,1);
				}
			},
			minus(index){
				//登录验证
				if(this.user == null){
					this.$router.push({path:'/login'});
					return;
				}
				if(this.foodArr[index].quantity>1){
					//update
					this.updateCart(index,-1);
				}else{
					//delete
					this.removeCart(index);
				}
			},
			saveCart(index){
				this.$axios.post('CartController/saveCart', this.$qs.stringify({
						businessId: this.businessId,
						userId: this.user.userId,
						footId: this.foodArr[index].foodId
					}))
					.then(response => {
						if (response.data == 1) {
							this.foodArr[index].quantity = 1;
							this.foodArr.sort();
						} else {
							alert('向购物车中添加食品失败');
						}
					})
					.catch(error => {
						console.error(error);
					});
			},
			updateCart (index,num) {
				this.$axios.post('CartController/updateCart', this.$qs.stringify({
						businessId: this.businessId,
						userId: this.user.userId,
						footId: this.foodArr[index].foodId,
						quantity:this.foodArr[index].quantity+num
					}))
					.then(response => {
						if (response.data == 1) {
							this.foodArr[index].quantity += num;
							this.foodArr.sort();
						} else {
							alert('向购物车中更新食品失败');
						}
					})
					.catch(error => {
						console.error(error);
					});
			},
			removeCart (index) {
				this.$axios.post('CartController/removeCart', this.$qs.stringify({
						businessId: this.businessId,
						userId: this.user.userId,
						footId: this.foodArr[index].foodId
					}))
					.then(response => {
						if (response.data == 1) {
							this.foodArr[index].quantity = 0;
							this.foodArr.sort();
						} else {
							alert('从购物车中删除食品失败');
						}
					})
					.catch(error => {
						console.error(error);
					});
			},*/
			//新增食品方法
			add(index) {
				//登陆验证
				if (this.user == null) {
					this.$router.push({
						path: '/login'
					});
					return;
				}
				if (this.foodArr[index].quantity == 0) {
					//insert
					this.savaCart(index);
				} else {
					//update
					this.updateCart(index, 1);
				}
			},
			//减去食品方法，为0时无法再减
			minus(index) {
				//登陆验证
				if (this.user == null) {
					this.$router.push({
						path: '/login'
					});
					return;
				}
			
				if (this.foodArr[index].quantity > 1) {
					//update
					this.updateCart(index, -1);
				} else {
					//delete
					this.removeCart(index);
				}
			}, 
			//
			savaCart(index) {
				this.$axios.post('CartController/saveCart', this.$qs.stringify({
					businessId: this.businessId,
					userId: this.user.userId,
					foodId: this.foodArr[index].foodId
				})).then(response => {
					if (response.data == 1) {
						//当前食品数量更新为1
						this.foodArr[index].quantity = 1;
						//trick，sort（）方法为编译方法的一种，使用编译方法使vue能够监听到数组的变化
						this.foodArr.sort();
					} else {
						alert('向购物车中添加食品失败！');
					}
				}).catch(error => {
					console.error(error);
				});
			}, 
			//更新方法
			updateCart(index, num) {
				this.$axios.post('CartController/updateCart', this.$qs.stringify({
					businessId: this.businessId,
					userId: this.user.userId,
					foodId: this.foodArr[index].foodId,
					quantity: this.foodArr[index].quantity + num
				})).then(response => {
					if (response.data == 1) {
						//当前食品数量更新为1或-1
						this.foodArr[index].quantity += num;
						//trick，sort（）方法为编译方法的一种，使用编译方法使vue能够监听到数组的变化
						this.foodArr.sort();
					} else {
						alert('向购物车中更新食品失败！');
					}
				}).catch(error => {
					console.error(error);
				});
			},
			//购物车删除食品方法
			removeCart(index) {
				this.$axios.post('CartController/removeCart', this.$qs.stringify({
					businessId: this.businessId,
					userId: this.user.userId,
					foodId: this.foodArr[index].foodId
				})).then(response => {
					if (response.data == 1) {
						//当前食品数量更新为0,视图的减号和数量要消失
						this.foodArr[index].quantity = 0;
						//trick，sort（）方法为编译方法的一种，使用编译方法使vue能够监听到数组的变化
						this.foodArr.sort();
					} else {
						alert('从购物车中删除食品失败！');
					}
				}).catch(error => {
					console.error(error);
				});
			},
			//列出用户历史购物车方法
			listCart() {
				this.$axios.post('CartController/listCart', this.$qs.stringify({
					businessId: this.businessId,
					userId: this.user.userId
				})).then(response => {
					//取出购物车列表
					let cartArr = response.data;
					//遍历所有食品列表
					for (let foodItem of this.foodArr) {
						foodItem.quantity = 0;
						for (let cartItem of cartArr) {
							if (cartItem.foodId == foodItem.foodId) {
								foodItem.quantity = cartItem.quantity;
							}
						}
					}
					this.foodArr.sort();
				}).catch(error => {
					console.error(error);
				});
			},
			toOrder(){
				this.$router.push({path:'order',query:{businessId:this.business.businessId}});
			}
		},
		computed:{
			//食品总价
			totalPrice(){
				let total=0;
				for(let item of this.foodArr){
					total += item.foodPrice*item.quantity;
				}
				return total;
			},
			//食品总数
			totalQuantity(){
				let quan=0;
				for(let item of this.foodArr){
					quan+=item.quantity;
				}
				return quan;
			},
			//结算价格
			totalSettle(){
				return this.totalPrice()+this.business.deliveryPrice;
			}
		}
		
	}
</script>
<!--
<script>
	import {
		ref,
		onMounted
	} from 'vue';

	export default {
		name: 'BusinessInfo',
		setup() {
			const businessId = ref(null);
			const business = ref({});
			const foodArr = ref([]);
			const user = ref(null);

			const add = (index) => {
				if (user.value === null) {
					this.$router.push({
						path: '/login'
					});
					return;
				}
				if (foodArr.value[index].quantity === 0) {
					// insert
					this.saveCart(index);
				} else {
					// update
					this.updateCart(index,1);
				}
			};

			const minus = (index) => {
				if (user.value === null) {
					this.$router.push({
						path: '/login'
					});
					return;
				}
				if (foodArr.value[index].quantity > 1) {
					// update
					this.updateCart(index,-1);
				} else {
					// delete
					this.removeCart(index);
				}
			};

			const saveCart = (index) => {
				this.$axios
					.post('CartController/saveCart', this.$qs.stringify({
						businessId: businessId.value,
						userId: user.value.userId,
						footId: foodArr.value[index].foodId
					}))
					.then(response => {
						if (response.data == 1) {
							foodArr.value[index].quantity = 1;
							this.foodArr.sort();
						} else {
							alert('向购物车中添加食品失败');
						}
					})
					.catch(error => {
						console.error(error);
					});
			};

			const updateCart = (index,num) => {
				this.$axios
					.post('CartController/updateCart', this.$qs.stringify({
						businessId: businessId.value,
						userId: user.value.userId,
						footId: foodArr.value[index].foodId,
						quantity:foodArr.value[index].quantity+num
					}))
					.then(response => {
						if (response.data == 1) {
							foodArr.value[index].quantity += num;
							this.foodArr.sort();
						} else {
							alert('向购物车中更新食品失败');
						}
					})
					.catch(error => {
						console.error(error);
					});
			};

			const removeCart = (index) => {
				this.$axios
					.post('CartController/removeCart', this.$qs.stringify({
						businessId: businessId.value,
						userId: user.value.userId,
						footId: foodArr.value[index].foodId
					}))
					.then(response => {
						if (response.data == 1) {
							foodArr.value[index].quantity = 0;
							this.foodArr.sort();
						} else {
							alert('从购物车中删除食品失败');
						}
					})
					.catch(error => {
						console.error(error);
					});
			};

			const listCart = () => {
				this.$axios
					.post('CartController/listCart', this.$qs.stringify({
						businessId: businessId.value,
						userId: user.value.userId,
					}))
					.then(response => {
						let cartArr = response.data;
						for(let foodItem of this.foodArr){
							foodItem.quantity = 0;
							for(let cartItem of cartArr){
								if(cartItem.foodId == foodItem.foodId){
									foodItem.quantity = cartItem.quantity;
								}
							}
						}
						this.foodArr.sort();
					})
					.catch(error => {
						console.error(error);
					});
			};
			onMounted(() => {
				user.value = this.$getSessionStorage('user');

				// 根据 businessId 查询商家信息
				this.$axios
					.post('BusinessController/getBusinessById', this.$qs.stringify({
						businessId: businessId.value
					}))
					.then(response => {
						business.value = response.data;
					})
					.catch(error => {
						console.error(error);
					});

				// 根据 businessId 查询食品信息
				this.$axios
					.post('FoodController/listFoodByBusinessId', this.$qs.stringify({
						businessId: businessId.value
					}))
					.then(response => {
						foodArr.value = response.data;
						foodArr.value.forEach(food => {
							food.quantity = 0;
						});
						if(this.user!=null){
							this.listCart();
						}
					})
					.catch(error => {
						console.error(error);
					});
			});

			return {
				businessId,
				business,
				foodArr,
				user,
				add,
				minus,
				saveCart
			};
		}
	};
</script>
-->

<style scoped>
	/****************总容器*********************/

	.wrapper {
		width: 100%;
		height: 100%;

	}

	/****************header*********************/
	.wrapper header {
		width: 100%;
		height: 12vw;
		background-color: #0097FF;
		color: #FFFFFF;
		font-size: 4.8vw;

		position: fixed;
		left: 0;
		top: 0;
		z-index: 1000;

		display: flex;
		align-items: center;
		justify-content: center;
	}

	.wrapper header .back {
		position: absolute;
		width: 2vw;
		height: 2vw;
		left: 1.5vw;
		top: 2.8vw;
		user-select: none;
		cursor: pointer;
	}

	/*****************logo********************/
	.wrapper .bsns-logo {
		width: 100%;
		height: 35vw;
		margin-top: 12vw;

		display: flex;
		justify-content: center;
		align-items: center;
	}

	.wrapper .bsns-logo img {
		width: 40vw;
		height: 30vw;
		border-radius: 5px;
	}

	/*****************info********************/
	.wrapper .bsns-info {
		width: 100%;
		height: 20vw;

		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.wrapper .bsns-info h1 {
		font-size: 5vw;
	}

	.wrapper .bsns-info p {
		font-size: 3vw;
		color: #666666;
		margin-top: 1vw;
	}

	/*****************foodList********************/
	.wrapper .foodList {
		width: 100%;
		display: inline-block;
		margin-bottom: 14vw;
	}

	.wrapper .foodList li {
		width: 100%;
		box-sizing: border-box;
		padding: 2.5vw;
		user-select: none;

		border-top: solid 0.3px #999999;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.wrapper .foodList li .food-l {
		display: flex;
		align-items: center;
	}

	.wrapper .foodList li .food-l img {
		width: 20vw;
		height: 20vw;
	}

	.wrapper .foodList li .food-l .food-l-info {
		margin-left: 3vw;
	}

	.wrapper .foodList li .food-l .food-l-info h3 {
		font-size: 3.8vw;
		color: #555555;
	}

	.wrapper .foodList li .food-l .food-l-info p {
		font-size: 3vw;
		color: #888888;
		margin-top: 2vw;
	}

	.wrapper .foodList li .food-r {
		width: 16vw;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.wrapper .foodList li .food-r .fa-minus-circle {
		font-size: 5.5vw;
		color: #999999;
		cursor: pointer;
	}

	.wrapper .foodList li .food-r p {
		font-size: 3.6vw;
		color: #333333;
	}

	.wrapper .foodList li .food-r .fa-plus-circle {
		font-size: 5.5vw;
		cursor: pointer;
		color: #0097EF;
	}

	/*********cart*********/
	.wrapper .cart {
		width: 100%;
		height: 14vw;

		position: fixed;
		left: 0;
		bottom: 0;

		display: flex;
	}

	.wrapper .cart .cart-l {
		flex: 2;
		background-color: #444444;
		display: flex;
	}

	.wrapper .cart .cart-l .cart-l-icon {
		width: 16vw;
		height: 16vw;
		box-sizing: border-box;
		border: solid 1.6vw #444444;
		border-radius: 8vw;
		background-color: #3190E8;
		font-size: 7vw;
		color: #FFFFFF;

		display: flex;
		justify-content: center;
		align-items: center;

		margin-top: -4vw;
		margin-left: 3vw;

		position: relative;
	}

	.wrapper .cart .cart-l .cart-l-icon-quantity {
		width: 5vw;
		height: 5vw;
		border-radius: 2.5vw;
		background-color: red;
		color: #FFFFFF;
		font-size: 3.6vw;

		display: flex;
		align-items: center;
		justify-content: center;

		position: absolute;
		right: -1.5vw;
		top: -1.5vw;
	}

	.wrapper .cart .cart-l .cart-l-info p {
		font-size: 4vw;
		color: #FFFFFF;
		margin-top: 1vw;
	}

	.wrapper .cart .cart-l .cart-l-info p:last-child {
		font-size: 2.8vw;
		color: #AAAAAA;
		margin-top: 0;
	}

	.wrapper .cart .cart-r {
		flex: 1;
		background-color: #38CA71;
	}

	.wrapper .cart .cart-r .cart-r-case1 {
		width: 100%;
		height: 100%;
		background-color: #666666;
		color: #FFFFFF;
		font-size: 4.5vw;
		font-weight: 700;
		user-select: none;

		display: flex;
		justify-content: center;
		align-items: center;
	}

	.wrapper .cart .cart-r .cart-r-case2 {
		width: 100%;
		height: 100%;
		background-color: #38CA73;
		color: #FFFFFF;
		font-size: 4.5vw;
		font-weight: 700;
		user-select: none;
		cursor: pointer;

		display: flex;
		justify-content: center;
		align-items: center;
	}
</style>
